package com.gwlc.common.base;


import com.gwlc.common.util.string.StringTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>lucky-com.gwlc.common.base-BaseErrorController</p>
 * <p>Description:  </p>
 * <p>Copyright: Copyright (c) 2021</p>
 * <p>Company: hanchunit@163.com</p>
 *
 * @author hanchun
 * @version 1.0
 * @date
 */


@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BaseErrorController extends BasicErrorController {

    @Value("${server.error.path:${error.path:/error}}")
    private String path;

    public BaseErrorController(ServerProperties serverProperties) {
        super(new DefaultErrorAttributes(), serverProperties.getError());
    }

    /**
     * 覆盖默认的HTML响应
     */
    @Override
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        HttpStatus status = getStatus(request);

        Map<String, Object> map = new HashMap<>(16);
        Map<String, Object> originalMsgMap = super.getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
        String path = (String) originalMsgMap.get("path");
        String error = (String) originalMsgMap.get("error");
        String message = (String) originalMsgMap.get("message");
        map.put("status", status.value());
        if (!StringTool.isNull(message)) {
            map.put("msg", message);
        }
        if (!StringTool.isNull(path)) {
            map.put("path", path);
        }
        if (!StringTool.isNull(error)) {
            map.put("error", error);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("meta", map);
        resultMap.put("data", null);
        return new ModelAndView("error", resultMap);
    }

    /**
     * 覆盖默认的JSON响应
     */
    @Override
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        HttpStatus status = getStatus(request);

        Map<String, Object> map = new HashMap<>(16);
        Map<String, Object> originalMsgMap = super.getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
        String path = (String) originalMsgMap.get("path");
        String error = (String) originalMsgMap.get("error");
        String message = (String) originalMsgMap.get("message");
        map.put("status", status.value());
        if (!StringTool.isNull(message)) {
            map.put("msg", message);
        }
        if (!StringTool.isNull(path)) {
            map.put("path", path);
        }
        if (!StringTool.isNull(error)) {
            map.put("error", error);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("meta", map);
        resultMap.put("data", null);
        return new ResponseEntity<>(resultMap, status);
    }

}
